package com.daiyang.campus_lucky_draw.service.activitystatus.operater;

import com.daiyang.campus_lucky_draw.common.enums.ActivityPrizeStatusEnum;
import com.daiyang.campus_lucky_draw.common.enums.ActivityStatusEnum;
import com.daiyang.campus_lucky_draw.mapper.ActivityMapper;
import com.daiyang.campus_lucky_draw.mapper.ActivityPrizeMapper;
import com.daiyang.campus_lucky_draw.mapper.DO.ActivityDO;
import com.daiyang.campus_lucky_draw.service.dto.ConvertActivityStatusDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ActivityOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
   public Boolean needCovert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum targetStatus = convertActivityStatusDTO.getTargetActivityStatus();
        if(activityId == null || targetStatus == null) {
            return false;
        }

        ActivityDO activityDO= activityMapper.selectById(activityId);
        if(activityDO == null) {
            return false;
        }
        //当前活动状态和传入的一直，不处理
        if(targetStatus.name().equals(activityDO.getStatus())) {
            return false;
        }
        // 需要判断奖品是否全部抽完
        // 查询 INIT 状态的奖品个数
        int count =activityPrizeMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name());
        if(count > 0) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityMapper.updateActivityStatus(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getTargetActivityStatus().name());
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
